package com.xxh.book.controller;

import com.xxh.book.dao.BookDao;
import com.xxh.book.entity.Book;
import com.xxh.book.utils.MbUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

@WebServlet(urlPatterns = "/book/*")
public class BookServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        String pathInfo = request.getPathInfo();
        HttpSession session = request.getSession();

        BookDao bookDao = MbUtil.getSqlSession().getMapper(BookDao.class);
        if(pathInfo.equals("/getAllBook")){
            List<Book> books = bookDao.findAll();
            session.setAttribute("book",books);
            response.sendRedirect(request.getContextPath()+"/book.jsp");
        }
        if(pathInfo.equals("/findBookByName")){
            String bookname = request.getParameter("bookname");
            List<Book> bookList = bookDao.findBookByName(bookname);
            session.setAttribute("book",bookList);
            response.sendRedirect(request.getContextPath()+"/book.jsp");
        }
        if(pathInfo.equals("/del")){
            String bidstr = request.getParameter("bid");
            int id = Integer.parseInt(bidstr);
            bookDao.deleteByPrimaryKey(id);
            response.sendRedirect(request.getContextPath()+"/book/getAllBook");
        }
        if(pathInfo.equals("/addBook")){
            Integer bno = Integer.parseInt(request.getParameter("bno"));
            String author = request.getParameter("author");
            String bname = request.getParameter("bname");
            String press = request.getParameter("press");
            Double price = Double.parseDouble(request.getParameter("price"));
            Integer typeID = Integer.parseInt(request.getParameter("type"));
            Book book = new Book(1,bno,bname,price,press,"非常好看",typeID,1,author,"");
            bookDao.insert(book);
            response.sendRedirect(request.getContextPath()+"/book/getAllBook");

        }

        if(pathInfo.equals("/getBookByID")){
            String bid = request.getParameter("bid");
            Book book = bookDao.selectByPrimaryKey(Integer.parseInt(bid));
            session.setAttribute("currentBook",book);
            response.sendRedirect(request.getContextPath()+"/updateDialog.jsp");
        }
        if(pathInfo.equals("/updateBook")){
            Integer id = Integer.parseInt(request.getParameter("id"));
            Integer bno = Integer.parseInt(request.getParameter("bno"));
            String author = request.getParameter("author");
            String bname = request.getParameter("bname");
            String press = request.getParameter("press");
            Double price = Double.parseDouble(request.getParameter("price"));
            Integer typeID = Integer.parseInt(request.getParameter("type"));
            Book book = new Book(id,bno,bname,price,press,"非常好看",typeID,1,author,"");
            bookDao.updateByPrimaryKey(book);
            response.sendRedirect(request.getContextPath()+"/book/getAllBook");
        }
    }
}
